const express = require('express')
const router = express.Router()
// 导入已封装的数据函数
const db = require('../../sql/db.js')
// 查询所有
router.get('/list',function(req,res){
    // 查询语句
    let sql = 'select * from info'
    // 调用查询方法
    db.query(sql,function(err, rows){
        if(err){
            res.send({code: -1, msg: '查询失败'})
        }else {
            res.send({
                code: 200,
                result: rows
            });
        }
    });
})
// 查询详情（id）
router.get('/detail',function(req,res){
    // 接收前端传递的用户ID
    let id = req.query.id;
    // console.log('xxx',id)
    // 查询语句
    let sql = 'select * from User where id = "'+id+'"';
    // 调用查询方法
    db.query(sql,function(err, rows){
        if(err){
            res.send({code: -1, msg: '查询失败'})
        }else {
            res.send({
                code: 200,
                result: rows
            });
        }
    });
})
// 修改数据
router.get('/update',function(req,res){
    let id = req.query.id;
    let passWord = req.query.passWord;
    let sql = 'update User set passWord = "'+passWord+'" where id="'+id+'"';
    db.query(sql, function (err) {
        if (err) {
            res.send({ code: -1, message: "修改失败" });
        } else {
            res.send({ code: 200, message: "修改成功" });
        }
    });

})
// 删除数据
router.get('/delete',function(req,res){
    let id = req.query.id;
     // "delete from  数据表 where 字段 = '" + 值 + "'"
    let sql = 'delete from User where id = "'+id+'"';
    db.query(sql,function(err){
        if(err){
            res.send({
                code: -1,
                msg: '删除失败'
            })
        }else {
            res.send({
                code: 200,
                msg: '删除成功'
            })
        }
    })
   
})

module.exports = router;